/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.chooser.filePreview;

import java.awt.Dimension;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;

import org.jxUtils.image.ImageUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.chooser.filePreview
 * @date 20/11/2014 - 12:37:33
 */
public class FilePreview extends JComponent implements PropertyChangeListener {
	
	private static final long serialVersionUID = 1998187377425091043L;
	
	private File file = null;
	
	private int height = 150;
	
	private ImageIcon thumbnail = null;
	
	private int width = 150;
	
	/**
	 * @param fileChooser
	 */
	public FilePreview(JFileChooser fileChooser) {
	
		setPreferredSize(new Dimension(width, height));
		
		fileChooser.addPropertyChangeListener(this);
	}
	
	/**
	 * @param fileChooser
	 * @param width
	 * @param height
	 */
	public FilePreview(JFileChooser fileChooser, int width, int height) {
	
		this.width = width;
		this.height = height;
		
		setPreferredSize(new Dimension(width, height));
		
		fileChooser.addPropertyChangeListener(this);
	}
	
	/**
	 * @param graphics
	 */
	@Override
	protected void paintComponent(Graphics graphics) {
	
		//
		if(thumbnail == null) {
			
			try {
				//
				loadPreview();
				
			}catch(IOException ioException) {
				
				ioException.printStackTrace();
			}
		}
		
		//
		if(thumbnail != null) {
			
			int x = (getWidth() / 2) - (thumbnail.getIconWidth() / 2);
			int y = (getHeight() / 2) - (thumbnail.getIconHeight() / 2);
			
			//
			if(y < 0) {
				
				y = 0;
			}
			
			//
			if(x < 5) {
				
				x = 5;
			}
			
			thumbnail.paintIcon(this, graphics, x, y);
		}
	}
	
	/**
	 * @throws IOException
	 */
	public void loadPreview() throws IOException {
	
		thumbnail = file == null ? null : ImageUtil.makePreviewToFile(file, width, height);
	}
	
	/**
	 * @param property
	 */
	@Override
	public void propertyChange(PropertyChangeEvent property) {
	
		boolean update = false;
		
		final String propertyName = property.getPropertyName();
		
		// If the directory changed, don't show an image.
		if(JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(propertyName)) {
			
			file = null;
			update = true;
			
			// If a file became selected, find out which one.
		}else if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(propertyName)) {
			
			file = (File)property.getNewValue();
			update = true;
		}
		
		// Update the preview accordingly.
		if(update) {
			
			thumbnail = null;
			
			//
			if(isShowing()) {
				
				try {
					//
					loadPreview();
					
				}catch(IOException ioException) {
					
					ioException.printStackTrace();
				}
				
				repaint();
			}
		}
	}
}
